
oo::define Charmm method getSegments {} {
    my variable molecules
    my processPsf
    return $molecules(segments)
}
oo::define Charmm method renameSegment { A B} {
  
}
oo::define Charmm method generate {} {

}

oo::define Charmm method processPsf {} {
    my variable molecules
    #get psf from Charmm
    set output [my sendCmd "print psf"] 
    #get sections start idx from psf 
    set idx_summary  [lindex [lindex [regexp -indices -inline -- {NATOM\s+NBOND\s+NTHETA\s+} $output] 0] 0]
    set idx_segments [lindex [lindex [regexp -indices -inline -- {PARTITION OF SEGMENTS} $output] 0] 0]
    set idx_atom_charateristics [lindex [lindex [regexp -indices -inline -- {ATOM\s+CHARACTERISTICS} $output] 0 ] 0]
    foreach res [regexp -indices -inline -all -- {RESIDUE} $output] {
       lappend idx_residues [lindex $res 0]
    } 

#   parse the summary informtion of the psf
    set str [string range $output $idx_summary [expr $idx_segments-1]]
    set str [regexp -all -inline -- {\S+} $str]
    for {set i 0} { $i < [expr [llength $str]/2]} {incr i} {
      set Vars([lindex $str $i]) [lindex $str [expr $i+[expr [llength $str]/2]]]
    }
#    parray Vars
    
#    parse the segments 
     set segments {}
    set str [string range $output $idx_segments [expr $idx_atom_charateristics-1]]
    foreach line [lrange [split $str \n] 2 end] {
      set str [regexp -all -inline -- {\S+} $line]
      if {[llength $str] >= 2} {
        set segment [lindex $str 1]
        lappend segments $segment
        set Vars($segment,NRES) [lindex $str 2]
      }
    }
#    parray Vars

#    parse residues connections
#    parse ...

#    update the molecules array in Charmm
     set molecules(segments) $segments
     foreach name [array names Vars] {
        set molecules($name) $Vars($name)
     }
}
